/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.funda.xmppserver.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kurinchi
 */
public class DBManager {

    static Connection conn;

    public static Connection getConn() {
        return conn;
    }

    public static void setConn(Connection conn) {
        DBManager.conn = conn;
    }

    public static String getDbName() {
        return dbName;
    }

    public static void setDbName(String dbName) {
        DBManager.dbName = dbName;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        DBManager.password = password;
    }

    public static String getUrl() {
        return url;
    }

    public static void setUrl(String url) {
        DBManager.url = url;
    }

    public static String getUserName() {
        return userName;
    }

    public static void setUserName(String userName) {
        DBManager.userName = userName;
    }
    static String url;
    static String dbName;
    static String driver;
    static String userName;
    static String password;

    public DBManager(Connection conn, String url, String dbName, String driver, String userName, String password) {
        DBManager.conn = conn;
        DBManager.url = url;
        DBManager.dbName = dbName;
        DBManager.driver = driver;
        DBManager.userName = userName;
        DBManager.password = password;

        initConnection();
    }

    public static void initConnection() {
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");
            //conn.close();
            //System.out.println("Disconnected from database");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updateChatHistory(String from, String to, String msg, String timestamp, boolean delay) {
        try {
            PreparedStatement prpsmt = conn.prepareStatement("insert into chathistory (fromid,fromjid,toid,msg,timestamp,deliveredtime,delay) values(?,?,?,?,?,?,?)");


            prpsmt.setString(1, from.substring(0, from.indexOf('@')));
            prpsmt.setString(2, from);
            prpsmt.setString(3, to);
            prpsmt.setString(4, msg);
            prpsmt.setString(5, timestamp);
            if (delay) {
                prpsmt.setString(6, "Pending...");
            } else {
                prpsmt.setString(6, timestamp);
            }
            prpsmt.setBoolean(7, delay);

            prpsmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void updateRoster(String userid, String subscription, String item) {
        try {
            PreparedStatement prpsmt = conn.prepareStatement("insert into roster values(?,?,?)");

            prpsmt.setString(1, userid);
            prpsmt.setString(2, subscription);
            prpsmt.setString(3, item);

            prpsmt.executeUpdate();

            if (subscription.equalsIgnoreCase("both")) {
                prpsmt = conn.prepareStatement("insert into roster values(?,?,?)");

                prpsmt.setString(1, item);
                prpsmt.setString(2, subscription);
                prpsmt.setString(3, userid);
                prpsmt.executeUpdate();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static boolean  updateUserdetails(String userid, String password, String email, String name) {
        try {

            Statement smt=conn.createStatement();
            ResultSet rs=smt.executeQuery("select * from userdetails where userid='"+userid+"'");

            System.out.println("User exists : "+rs.next());
            if(rs.next())
            {
                return false;
            }

            PreparedStatement prpsmt = conn.prepareStatement("insert into userdetails (userid,password,Name,email) values(?,?,?,?)");

            prpsmt.setString(1, userid);
            prpsmt.setString(2, password);
            prpsmt.setString(3, name);
            prpsmt.setString(4, email);

            prpsmt.executeUpdate();


        } catch (SQLException ex) {
            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
}
